package com.twp.auth.domain.authority;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@EqualsAndHashCode(
        exclude = {
                "childrenRef","requestUrlList","roleList"
        }
)
@ToString(
        exclude = {
                "childrenRef","requestUrlList","roleList"
        }
)
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(name = "twp_menu")
public class Menu implements Serializable{
    /**
     * 编号
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    /**
     * 菜单名称
     */
    private String title;

    private String code;

    private String router;

    /**
     * 图标
     */
    private String icon;

    /**子组织*/
    @OrderBy("menuOrder asc ")
    @OneToMany
    @JoinColumn(name="parent_id")
    @JsonIgnore
    private Set<Menu> childrenRef = new HashSet<Menu>();

    @Transient
    private Set<Menu> children = new HashSet<Menu>();

    public Set<Menu> getChildren() {
        if (children==null) return null;
        if (children.isEmpty()){
            children = this.getChildrenRef();
        }
        if (this.getCurrentRoleId()!=null){
            this.children.stream().forEach(
                    c->c.setCurrentRoleId(currentRoleId)
            );
        }
        return children;
    }

    @Transient
    private Integer parentId;

    /**
     * 菜单显示顺序
     */
    @Column(name = "menuOrder")
    private Integer menuOrder;

    /**
     * 1隐藏，0显示
     */
    private Boolean status;

    /**
     * 更新时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "updateTime")
    private Date updateTime;

    /**
     * 修改人
     */
    @Column(name = "updateUser")
    private String updateUser;

    private Integer level;

    @JsonIgnore
    @JoinTable(name="twp_menu_requestUrl",//中间表的名称
            joinColumns={@JoinColumn(name="menuId",referencedColumnName="ID")},//中间表PRODUCT_ID字段关联PRODUCT的ID
            inverseJoinColumns={@JoinColumn(name="requestUrlId",referencedColumnName="ID")})//中间表CATEGORY_ID字段关联CATEGORY的ID
    @ManyToMany(fetch = FetchType.LAZY)
    private Set<RequestUrl> requestUrlList;

    @JsonIgnore
    @ManyToMany(mappedBy = "menuList",fetch = FetchType.LAZY)
    private Set<Role> roleList;

    @Transient
    private boolean checked = false;
    @Transient
    private Integer currentRoleId;

    @Transient
    private boolean expand = true;

    public boolean isChecked() {
        if (roleList.isEmpty()) return false;
        try {
            return roleList.stream().anyMatch(role -> role.getId().equals(this.currentRoleId));
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
